package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.domain.BorrowerCompany;
import com.edais.service.BorrowerCompanyService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;

/** 订单action */
@Controller
@RequestMapping("/borrowerCompany")

public class BorrowerCompanyAction implements Serializable {
	/**
	 * @Fields serialVersionUID : 序列号id
	 */
	private static final long serialVersionUID = 7986499326379245049L;

	/** 日志 */
	private static final Log logger = LogFactory
			.getLog(BorrowerCompanyAction.class);

	/** 设置service */
	@Autowired
	private BorrowerCompanyService borrowerCompanyService;

	/**
	 * 
	 * @Description: 根据订单id查询借款人详情
	 * @param request
	 * @param response
	 * @return void
	 * @throws
	 * @author qibo
	 * @date 2015-9-4 下午5:21:41
	 */
	@RequestMapping(value = "/getBorrowerCompanyInfo")
	public void getBorrowerCompanyInfo(HttpServletRequest request,
			HttpServletResponse response) {
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		//上架表id
		String sloanId = paramsMap.get("sloanId");
		//投资状态
		String investType = paramsMap.get("investType");
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		// 程序必须参数未传入
		if (StringUtils.isBlank(sloanId)) {
			logger.error("获取投资列表信息数据检查失败:上架表id为空");
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			return;
		}
		if (StringUtils.isBlank(investType)) {
			investType = "0";
		}
		
		try {
			//查询借款人公司信息
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("sloanId", sloanId);
			List<BorrowerCompany> borrowerCompanyList = this.borrowerCompanyService.getBorrowerCompanyInfo(map);
			if (borrowerCompanyList != null && borrowerCompanyList.size() > 0) {
				BorrowerCompany borrowerCompany = borrowerCompanyList.get(0);
				//自增id
				jsonMap.put("id", borrowerCompany.getId());
				//借款人公司id
				jsonMap.put("companyId", borrowerCompany.getCompanyId());
				//借款人公司名称
				jsonMap.put("companyName", borrowerCompany.getCompanyName() == null ? "" : borrowerCompany.getCompanyName());
				//借款方简介
				jsonMap.put("borrowerIntroduction", borrowerCompany.getBorrowerIntroduction() == null ? "" : borrowerCompany.getBorrowerIntroduction());
				//担保方简介
				jsonMap.put("guarantorIntroduction", borrowerCompany.getGuarantorIntroduction() == null ? "" : borrowerCompany.getGuarantorIntroduction());
				//资金保障 
				jsonMap.put("fundSecurity", borrowerCompany.getFundSecurity() == null ? "" : borrowerCompany.getFundSecurity());
				//资金保障 
				jsonMap.put("fundSecurityNew", borrowerCompany.getFundSecurityNew() == null ? "" : borrowerCompany.getFundSecurityNew());
				//项目详情
				jsonMap.put("projectDetails", borrowerCompany.getProjectDetails() == null ? "" : borrowerCompany.getProjectDetails());
			} else {
				//自增id
				jsonMap.put("id", "");
				//借款人公司id
				jsonMap.put("companyId", "");
				//借款人公司名称
				jsonMap.put("companyName", "");
				//借款方简介
				jsonMap.put("borrowerIntroduction", "");
				//担保方简介
				jsonMap.put("guarantorIntroduction", "");
				//资金保障
				jsonMap.put("fundSecurity", "");
				//资金保障
				jsonMap.put("fundSecurityNew", "");
				//项目详情
				jsonMap.put("projectDetails", "");
			}
			
			//设置返回结果
			String result = CommonUtil.setResultStringCn(
					jsonMap, Consts.SUCCESS_CODE, "","");

			// 处理返回结果
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("borrowerCompany/getBorrowerCompanyInfo 异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
}
